#include <iostream>
#include <SFML/Graphics.hpp>
#include "../../include/GUI/Onglets.hpp"
#include "../../include/GUI/Button.hpp"

using namespace std;

namespace GUI
{

    Onglets::Onglets(Engine &Moteur) : Widget(Moteur)
    {
        m_image.SetImage(*m_Moteur.Graphic()->GetGUIImage(5));

        m_rects.push_back(sf::IntRect(0,28,m_padding,m_padding)); // 0 - Coin haut gauche
        m_rects.push_back(sf::IntRect(m_padding,28,162-2*m_padding,m_padding)); // 1 - Haut
        m_rects.push_back(sf::IntRect(162-m_padding,28,m_padding,m_padding)); // 2 - Coin haut droite
        m_rects.push_back(sf::IntRect(0,28+m_padding,m_padding,118-2*m_padding-28)); // 3 - Gauche
        m_rects.push_back(sf::IntRect(m_padding,28+m_padding,162-2*m_padding,118-2*m_padding-28)); // 4 - Milieu
        m_rects.push_back(sf::IntRect(162-m_padding,28+m_padding,m_padding,118-28-2*m_padding)); // 5 - Droite
        m_rects.push_back(sf::IntRect(0,118-m_padding,m_padding,m_padding)); // 6 - Coin bas gauche
        m_rects.push_back(sf::IntRect(m_padding,118-m_padding,162-2*m_padding,m_padding)); // 7 - Bas
        m_rects.push_back(sf::IntRect(162-m_padding,118-m_padding,m_padding,m_padding)); // 8 - Coin bas droite

        m_focus = 0; // Le premier onglet a le focus pas défaut

        sf::Font m_font = m_Moteur.Config()->GetGUI()->font_contenu;
    }

    void Onglets::AddOnglet(const sf::String &text)
    {
        m_onglets.push_back(new Onglet(m_Moteur, text));
        m_widgets.push_back(std::vector<Widget*>());

        m_onglets[0]->SetFocus(true); // Le premier onglet a le focus
    }
    void Onglets::AddWidget(const unsigned int id_onglet, Widget *widget)
    {
        if(id_onglet < m_widgets.size()) // L'onglet existe
        {
            m_widgets[id_onglet].push_back(widget);

            int pos_x = m_position.x+m_widgets[id_onglet].back()->GetConteneurPosition().x+m_padding;
            int pos_y = m_position.y+m_widgets[id_onglet].back()->GetConteneurPosition().y+m_padding+28;

            m_widgets[id_onglet].back()->SetPosition(pos_x,pos_y);
            m_widgets[id_onglet].back()->SetParent(this);
        }
    }

    void Onglets::Adjust()
    {
        // Hauteur et largeur de l'objet actuellement analyser
        int width = 0;
        int height = 0;
        // Hauteur et largeur maximal que la fenêtre prendra grâce aux widgets
        int max_width = 0;
        int max_height = 0;

        for(unsigned int i=0;i<m_widgets.size();i++)
        {
            for(unsigned int j=0;j<m_widgets[i].size();j++)
            {
                width = m_widgets[i][j]->GetConteneurPosition().x + m_widgets[i][j]->GetDimensions().width;
                height = m_widgets[i][j]->GetConteneurPosition().y + m_widgets[i][j]->GetDimensions().height+2*m_padding+28;
                if(width > max_width)
                    max_width = width;
                if(height > max_height)
                    max_height = height;
            }
        }

        if(m_dimensions.width < max_width)
            m_dimensions.width = max_width;
        else
            m_dimensions.width = max_width+2*m_padding;

        if(m_dimensions.width < 81*m_widgets.size())
            m_dimensions.width = 82*m_widgets.size()-2*m_padding-1;

        m_dimensions.height = max_height;
    }

    void Onglets::Afficher()
    {
        // Coin haut gauche
        m_image.SetPosition(m_position.x, m_position.y+28);
        m_image.SetSubRect(m_rects[0]);
        m_image.SetScale(1,1);
        m_Moteur.Graphic()->Draw(m_image);

        // Haut
        m_image.SetPosition(m_position.x+m_padding, m_position.y+28);
        m_image.SetSubRect(m_rects[1]);
        m_image.SetScale(m_dimensions.width/(162-2*m_padding),1);
        m_Moteur.Graphic()->Draw(m_image);

        // Coin haut droite
        m_image.SetPosition(m_position.x+m_dimensions.width+m_padding, m_position.y+28);
        m_image.SetSubRect(m_rects[2]);
        m_image.SetScale(1,1);
        m_Moteur.Graphic()->Draw(m_image);

        // Gauche
        m_image.SetPosition(m_position.x, m_position.y+28+m_padding);
        m_image.SetSubRect(m_rects[3]);
        m_image.SetScale(1,(m_dimensions.height-2*m_padding-28)/(118-28-2*m_padding));
        m_Moteur.Graphic()->Draw(m_image);

        // Centre
        m_image.SetPosition(m_position.x+m_padding, m_position.y+28+m_padding);
        m_image.SetSubRect(m_rects[4]);
        m_image.SetScale(m_dimensions.width/(162-2*m_padding),(m_dimensions.height-2*m_padding-28)/(118-28-2*m_padding));
        m_Moteur.Graphic()->Draw(m_image);

        // Droite
        m_image.SetPosition(m_position.x+m_dimensions.width+m_padding, m_position.y+28+m_padding);
        m_image.SetSubRect(m_rects[5]);
        m_image.SetScale(1,(m_dimensions.height-2*m_padding-28)/(118-28-2*m_padding));
        m_Moteur.Graphic()->Draw(m_image);

        // Coin bas gauche
        m_image.SetPosition(m_position.x, m_position.y+m_dimensions.height-m_padding);
        m_image.SetSubRect(m_rects[6]);
        m_image.SetScale(1,1);
        m_Moteur.Graphic()->Draw(m_image);

        // Bas
        m_image.SetPosition(m_position.x+m_padding, m_position.y+m_dimensions.height-m_padding);
        m_image.SetSubRect(m_rects[7]);
        m_image.SetScale(m_dimensions.width/(162-2*m_padding),1);
        m_Moteur.Graphic()->Draw(m_image);

        // Coin bas droite
        m_image.SetPosition(m_position.x+m_padding+m_dimensions.width, m_position.y-m_padding+m_dimensions.height);
        m_image.SetSubRect(m_rects[8]);
        m_image.SetScale(1,1);
        m_Moteur.Graphic()->Draw(m_image);

        for(unsigned int i=0;i<m_onglets.size();i++)
        {
            int pos_x = 0;
            int pos_y = 0;

            pos_x = m_position.x+82*i;
            pos_y = m_position.y;

            if(m_onglets[i]->IsFocused())
                pos_y++;

            m_onglets[i]->SetPosition(pos_x, pos_y);
            m_onglets[i]->Afficher();

            if(m_onglets[i]->IsFocused())
            {
                for(unsigned int j=0; j<m_widgets[i].size(); j++)
                {
                    pos_x = m_position.x+m_widgets[i][j]->GetConteneurPosition().x+m_padding;
                    pos_y = m_position.y+m_widgets[i][j]->GetConteneurPosition().y+m_padding+28;

                    m_widgets[i][j]->SetPosition(pos_x,pos_y);
                    m_widgets[i][j]->Afficher();
                }
            }
        }
    }

    void Onglets::UpdateEvent()
    {
        for(unsigned int i=0; i<m_onglets.size(); i++)
        {
            m_onglets[i]->UpdateEvent();

            if(m_onglets[i]->IsClicked())
            {
                m_onglets[m_focus]->SetFocus(false);
                m_onglets[i]->SetFocus(true);
                m_focus = i;
            }
            if(m_onglets[i]->IsFocused())
            {
                if(!m_widgets.empty())
                {
                    for(unsigned int j=0; j<m_widgets[i].size(); j++)
                    {
                        m_widgets[i][j]->UpdateEvent();
                    }
                }
            }
        }
    }

    Onglets::~Onglets()
    {
        for(size_t i = 0; i < m_onglets.size(); i++)
        {
            delete(m_onglets[i]);
        }
    }

}
